<?php
namespace app\adminjcb\controller;

use app\common\controller\AdminBaseController;

/**
 * Class Member
 */
class TixianController extends AdminBaseController {

	/**
	 * @param string $keyword
	 * @param int    $page
	 * @return mixed
	 */
	public function index($keyword = '', $ziduan = 'id') {
		$map['p.status'] = 1;
		if ($keyword) {
            $map['m.'.$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$this->assign('keyword', $keyword);
		$list = db('tixian p')->field('m.mobile,m.real_name,m.bank_name,m.bank_no,m.alipay,p.*')->join('member m', 'p.uid=m.id')->where($map)->order('p.add_time DESC')->paginate(15, false, get_query());
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function all($keyword = '', $ziduan = 'id') {
		$map['p.status'] = 2;
		if ($keyword) {
            $map['m.'.$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		$this->assign('keyword', $keyword);
		$list = db('tixian p')->field('m.mobile,m.real_name,m.bank_name,m.bank_no,m.alipay,p.*')->join('member m', 'p.uid=m.id')->where($map)->order('p.add_time DESC')->paginate(15, false, get_query());
		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * @param $id
	 */
	public function delete($id) {
		if (db('tixian')->delete($id)) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}

	/*
	 * 完成
	 */
	public function complete($id) {
		$data['tixian_time'] = now_time();
		$data['status'] = 2;
		$result = db('tixian')->where('id', $id)->update($data);
		$tixian = db('tixian')->find($id);
		// 减少钱
		$result2 = db("member")->where('id', $tixian['uid'])->setDec('frozen_money', $tixian['money']);
		if ($result && $result2) {
			add_money_log('交易消费', '账户余额提现成功支出', 0 - $tixian['money'], $tixian['uid'], 2);
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}

	/*
	 * 取消
	 */
	public function cancel($id) {
		$data['tixian_time'] = now_time();
		$data['status'] = 3;
		$result = db('tixian')->where('id', $id)->update($data);
		// 解除冻结增加余额
		$money = db('tixian')->where('id', $id)->value('money');
		$result2 = db("member")->where('id', $id)->setInc('money', $money);
		$result3 = db("member")->where('id', $id)->setDec('frozen_money', $money);
		if ($result && $result2 && $result3) {
			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}

}
